// pages/mine/mine.js
const app = getApp()
const api = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    avatarUrl: "",
    nickName: '',
    gender: null,
    genderImg: '',
    province: '', //地址
    openid: '',
    ownSign: '', //个性签名
    userInfo: {}
  },
  
  dealOrder(){
    if (!app.globalData.isLogin) {
      wx.showToast({
        title: '请登录后操作',
        icon: "none"
      })
      return
    }
    wx.navigateTo({
      url: '../dealOrder/dealOrder',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!app.globalData.isLogin) {
      wx.showToast({
        title: '请登录后操作',
        icon: "none"
      })
      return
    }
    this.setData({
      isLogin: app.globalData.isLogin,
      avatarUrl: app.globalData.userInfo.avatarUrl,
      nickName: app.globalData.userInfo.nickName,
      ownSign: app.globalData.userInfo.pro ? app.globalData.userInfo.pro : '个性签名',
      gender: app.globalData.userInfo.gender
    })
  },
  // getOpenid(){
  //   wx.cloud.callFunction({
  //     name:"login",
  //     success:res=>{
  //       console.log(res)
  //       const openid = res.result.openid
  //       wx.setStorageSync('openid', openid)
  //     }
  //   })
  // },
  getUserProfile(e) {
    // this.getOpenid()
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          avatarUrl: res.userInfo.avatarUrl,
          nickName: res.userInfo.nickName,
          gender: res.userInfo.gender,
          province: res.userInfo.province,
          isLogin: true
        })
        app.globalData.userInfo = res.userInfo;
        wx.request({
          url: api.regsUser.url,
          method: api.regsUser.method,
          data: {
            avatarUrl: res.userInfo.avatarUrl,
            nickName: res.userInfo.nickName,
            gender: res.userInfo.gender,
            province: res.userInfo.province,
            openid: app.globalData.openid
          },
          success: res => {
            console.log(res)
            //改变全局的isLogin
            app.globalData.isLogin = true

          }
        })
      }
    })
  },
  topUpdate: function () {
    wx.navigateTo({
      url: '../mineUpdate/mineUpdate?avatarUrl=' + this.data.avatarUrl + '&nickName=' + this.data.nickName + '&gender=' + this.data.gender + '&ownSign=' + this.data.ownSign,
    })
  },
  goWait(e) {
    if (!app.globalData.isLogin) {
      wx.showToast({
        title: '请登录后操作',
        icon: "none"
      })
      return
    }
    console.log(e.currentTarget.dataset.active)
    wx.navigateTo({
      url: '../myMsg/myMsg?active=' + e.currentTarget.dataset.active,
    })
  },
  goSend(e) {
    if (!app.globalData.isLogin) {
      wx.showToast({
        title: '请登录后操作',
        icon: "none"
      })
      return
    }
    wx.navigateTo({
      url: '../myMsg/myMsg?active=' + e.currentTarget.dataset.active,
    })
  },
  goComplete(e) {
    if (!app.globalData.isLogin) {
      wx.showToast({
        title: '请登录后操作',
        icon: "none"
      })
      return
    }
    wx.navigateTo({
      url: '../myMsg/myMsg?active=' + e.currentTarget.dataset.active,
    })
  },
  goMySend(e) {
    if (!app.globalData.isLogin) {
      wx.showToast({
        title: '请登录后操作',
        icon: "none"
      })
      return
    }
    wx.navigateTo({
      url: '../myMsg/myMsg?active=' + e.currentTarget.dataset.active,
    })
  },
  onShow() {
    if (!app.globalData.isLogin) {
      wx.showToast({
        title: '请登录后操作',
        icon: "none"
      })
      return
    }
    wx.request({
      url: api.getUserInfo.url,
      method: api.getUserInfo.method,
      data: {
        openid: app.globalData.openid
      },
      success: res => {
        console.log(res.data.data)
        this.setData({
          avatarUrl: res.data.data[0].avatarUrl,
          nickName: res.data.data[0].nickName,
          gender: res.data.data[0].gender,
          ownSign: res.data.data[0].pro
        })
      },

    })
  }
})